package com.supor.aiot.mine.ui.setting;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.android.baseconfig.common.utils.Logc;
import com.supor.aiot.common.app.SuporApplication;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.api.AppReleaseImpl;
import com.supor.aiot.mine.bean.AppReleaseItem;
import com.supor.aiot.mine.bean.BossTAppParams;
import com.supor.aiot.mine.bean.FunctionIntroductionItem;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class FunctionIntroductionViewModel extends BaseViewModel {

    private MutableLiveData<Result> updateResult = new MutableLiveData<>();

    public MutableLiveData<Result> getUpdateResult() {
        return updateResult;
    }

    public FunctionIntroductionViewModel(@NonNull Application application) {
        super(application);
    }

    @SuppressLint("CheckResult")
    public void getAppReleaseAll() {
        AppReleaseImpl.getInstance().getAppReleaseListPage(BossTAppParams.appOs,
                BossTAppParams.appType, BossTAppParams.page, BossTAppParams.limit).subscribe(result -> {
            if (result.isSuccess()) {
                BossTAppParams.total = result.total;
                List<FunctionIntroductionItem> introductionItems = new ArrayList<>();
                StringBuilder sb = new StringBuilder();
                for (AppReleaseItem appReleaseItem : result.data.rows) {
                    sb.setLength(0);
                    String[] ts = appReleaseItem.releaseDate.split(" ");
                    if (ts != null && ts.length > 0) {
                        String[] dates = ts[0].split("-");
                        Calendar cd = Calendar.getInstance();
                        //判断是否为今年，如果是 隐藏年份
                        sb.append(dates[0]).append("年");
                        if (String.valueOf(cd.get(Calendar.YEAR)).equals(dates[0])) {
                            sb.setLength(0);
                        }
                        sb.append(dates[1]).append("月").append(dates[2]).append("日");
                        //判断是否为今日，如果是 隐藏日期
                        if (String.valueOf(cd.get(Calendar.DATE)).equals(dates[2])) {
                            sb.setLength(0);
                        }

                        // 防止接口返回数据只有日期
                        if (ts.length > 1) {
                            String[] times = ts[1].split(":");
                            sb.append(" ").append(times[0]).append(":").append(times[1]);
                        }
                    }
                    introductionItems.add(new FunctionIntroductionItem(SuporApplication.getInstance().getString(R.string.app_name)
                            + appReleaseItem.appVersion + "主要更新", sb.toString(), appReleaseItem.versionComment));
                }
                //取消降序
                //Collections.reverse(introductionItems);
                updateResult.setValue(new Result.Success(introductionItems));
            } else {
                updateResult.setValue(new Result.Error("getAppReleaseAll failed"));
            }
        }, throwable -> {
            Logc.e("getAppReleaseAll: " + throwable);
            updateResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }

}
